import type { RouteObject } from 'react-router-dom'
import { createBrowserRouter, redirect } from 'react-router-dom'
import App from '../App'
import Login from '../pages/register/Login'
import Register from '../pages/register/Register'
import Guide from '../pages/guide/Guide'
import Home from '../pages/tabs/Home'
import Training from '../pages/tabs/Training'
import Data from '../pages/tabs/Data'
import Profile from '../pages/tabs/Profile'
import Gender from '../pages/shizhiran/Gender'
import Goal from '../pages/shizhiran/Goal'
import Height from '../pages/shizhiran/Height'
import Weight from '../pages/shizhiran/Weight'
import TargetWeight from '../pages/shizhiran/TargetWeight'
import Interest from '../pages/shizhiran/Interest'
import TrainingLevel from '../pages/shizhiran/TrainingLevel'
import GeneratingPlan from '../pages/shizhiran/GeneratingPlan'

import Mytraining from '../pages/mytraining/Mytraining'
import SetupTraining from '../pages/mytraining/set/SetupTraining'
import SetMy from '../pages/mytraining/set/setMy'
import SetMyDetail from '../pages/mytraining/set/SetMyDetail'
import Individual from '../components/Mypage/childern/individual'
import Trainings from '../components/Mypage/childern/trainings'
import Remind from '../components/Mypage/childern/remind'
import Help from '../components/Mypage/childern/help'
import CustomerService from '../components/Mypage/childern/CustomerService'
import { authManager } from '../utils/auth'

// 路由守卫：检查用户是否已登录
const authGuard = () => {
  if (!authManager.isAuthenticated()) {
    return redirect('/login')
  }
  return null
}

import AllCategories from '../pages/home/AllCategories'
import Notifications from '../pages/home/Notifications'
import ShapeTraining from '../pages/home/ShapeTraining'
import TrainingDetail from '../pages/home/TrainingDetail'
import TrainingSession from '../pages/training/TrainingSession'
import ScheduleTraining from '../pages/training/ScheduleTraining'
import TrainingManagement from '../pages/training/TrainingManagement'
import Sport from '../pages/tabs/Sport'

// 根路由：根据是否登录进行重定向
const rootRedirect = () => {
  return authManager.isAuthenticated() ? redirect('/index/home') : redirect('/login')
}

// 仅未登录可访问（例如登录/注册页）
const publicOnlyLoader = () => {
  return authManager.isAuthenticated() ? redirect('/index/home') : null
}

const routes: RouteObject[] = [
  {
    path: '/',
    loader: rootRedirect,
  },
  {
    path: '/guide',
    Component: Guide,
    loader: authGuard, // 需要登录才能访问
  },
  {
    path: '/gender',
    Component: Gender,
    loader: authGuard,
  },
  {
    path: '/goal',
    Component: Goal,
    loader: authGuard,
  },
  {
    path: '/height',
    Component: Height,
    loader: authGuard,
  },
  {
    path: '/weight',
    Component: Weight,
    loader: authGuard,
  },
  {
    path: '/targetweight',
    Component: TargetWeight,
    loader: authGuard,
  },
  {
    path: '/interest',
    Component: Interest,
    loader: authGuard,
  },
  {
    path: '/traininglevel',
    Component: TrainingLevel,
    loader: authGuard,
  },
  {
    path: '/generatingplan',
    Component: GeneratingPlan,
    loader: authGuard,
  },
  {
    path: '/register',
    Component: Register,
    loader: publicOnlyLoader,
  },
  {
    path: '/categories',
    Component: AllCategories,
    loader: authGuard,
  },
  {
    path: '/notifications',
    Component: Notifications,
    loader: authGuard,
  },
  {
    path: '/shape',
    Component: ShapeTraining,
    loader: authGuard,
  },
  {
    path: '/training-detail',
    Component: TrainingDetail,
    loader: authGuard,
  },
  {
    path: '/training-session',
    Component: TrainingSession,
    loader: authGuard,
  },
  {
    path: '/schedule-training',
    Component: ScheduleTraining,
    loader: authGuard,
  },
  {
    path: '/training-management',
    Component: TrainingManagement,
  },
  {
    path: '/index',
    Component: App,
    loader: authGuard, // 需要登录才能访问
    children: [
      { index: true, loader: () => redirect('/index/home') },
      { path: 'home', Component: Home },
      { path: 'training', Component: Training },
      { path: 'sport', Component: Sport },
      { path: 'data', Component: Data },
      { 
        path: 'profile', 
        Component: Profile,
        children: [
          { path: 'individual', Component: Individual },
          { path: 'trainings', Component: Trainings },
          { path: 'remind', Component: Remind },
          { path: 'help', Component: Help },
        ]
      },
      
    ],
  },
  {
    path: '/login',
    Component: Login,
    loader: publicOnlyLoader,
  },
  {
    path: '/register',
    Component: Register,
  },
  { path: '/mytraining', Component: Mytraining },
  { path: '/mytraining/set', Component: SetupTraining },
  { path: '/mytraining/setmy', Component: SetMy },
  { path: '/mytraining/setmy/:id', Component: SetMyDetail },
  { path: '/customer-service', Component: CustomerService, loader: authGuard },
]

export default createBrowserRouter(routes)
